// -----------------------------------------------------------------------
//  <copyright file="DictionaryTypeControllerBase.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-10-17 01:10</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore.Mvc.Filters;
using DaprPlus.DataDicts.Application.Commands;
using DaprPlus.DataDicts.Application.Queries;
using DaprPlus.DataDicts.Domain.Dtos;
using DaprPlus.Domain;

using OSharp.Filter;


namespace DaprPlus.DataDicts.Controllers;

[ApiResultWrapperFilter]
[Route("api/dict")]
public abstract class DictionaryTypeControllerBase(IMediator mediator) : ApiControllerBase
{
    /// <summary>
    /// 分页查询字典类型
    /// </summary>
    /// <param name="query">查询条件</param>
    /// <returns>分页结果</returns>
    [HttpPost("read")]
    [Produces<PageResult<DictionaryTypeOutDto>>]
    public virtual async Task<IActionResult> Read([FromBody] GetDictionaryTypePageQuery query)
    {
        var page = await mediator.Send(query);
        return Ok(page);
    }

    /// <summary>
    /// 获取字典类型节点列表
    /// </summary>
    /// <param name="keyword">关键字</param>
    /// <returns>节点列表</returns>
    [HttpGet("node")]
    [Produces<DictionaryTypeNodeOutDto[]>]
    public virtual async Task<IActionResult> GetNode(string? keyword = null)
    {
        var query = new GetDictionaryTypeNodeQuery(keyword);
        var result = await mediator.Send(query);
        return Ok(result);
    }

    /// <summary>
    /// 创建字典类型
    /// </summary>
    /// <param name="command">创建命令</param>
    /// <returns>创建结果</returns>
    [HttpPost]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Create([FromBody] CreateDictionaryTypeCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// 更新字典类型
    /// </summary>
    /// <param name="command">更新命令</param>
    /// <returns>更新结果</returns>
    [HttpPut]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Update([FromBody] UpdateDictionaryTypeCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// 删除字典类型
    /// </summary>
    /// <param name="ids">删除Id集合</param>
    /// <returns>删除结果</returns>
    [HttpDelete]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Delete(long[] ids)
    {
        var command = new DeleteDictionaryTypeCommand(ids);
        var result = await mediator.Send(command);
        return Ok(result);
    }

    #region 字典项管理

    /// <summary>
    /// 分页查询字典项
    /// </summary>
    /// <param name="query">查询条件</param>
    /// <returns>分页结果</returns>
    [HttpPost("item/read")]
    [Produces<PageResult<DictionaryItemOutDto>>]
    public virtual async Task<IActionResult> ReadItems([FromBody] GetDictionaryItemPageQuery query)
    {
        var page = await mediator.Send(query);
        return Ok(page);
    }

    /// <summary>
    /// 获取字典项节点列表
    /// </summary>
    /// <param name="typeCode">字典类型代码</param>
    /// <param name="keyword">关键字</param>
    /// <returns>节点列表</returns>
    [HttpGet("item/node")]
    [Produces<DictionaryItemNodeOutDto[]>]
    public virtual async Task<IActionResult> GetItemNode(string typeCode, string? keyword = null)
    {
        var query = new GetDictionaryItemNodeQuery(typeCode, keyword);
        var result = await mediator.Send(query);
        return Ok(result);
    }

    /// <summary>
    /// 创建字典项
    /// </summary>
    /// <param name="command">创建命令</param>
    /// <returns>创建结果</returns>
    [HttpPost("item")]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> CreateItem([FromBody] CreateDictionaryItemCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// 更新字典项
    /// </summary>
    /// <param name="command">更新命令</param>
    /// <returns>更新结果</returns>
    [HttpPut("item")]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> UpdateItem([FromBody] UpdateDictionaryItemCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    /// <summary>
    /// 删除字典项
    /// </summary>
    /// <param name="command">删除命令</param>
    /// <returns>删除结果</returns>
    [HttpDelete("item")]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> DeleteItem([FromBody] DeleteDictionaryItemCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    #endregion
}
